﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QLChoDiaOc.Process;

namespace QLChoDiaOc.Forms
{
    /// <summary>
    /// Interaction logic for FormGiaHanPhieuThu.xaml
    /// </summary>
    public partial class FormGiaHanPhieuThu : Page
    {
        SelectInfo _selectInfo = new SelectInfo();
        InsertInfo _insertInfo = new InsertInfo();
        GetID _getID = new GetID();

        public FormGiaHanPhieuThu()
        {
            InitializeComponent();
            AddComboBox();
        }

        private void AddComboBox()
        {
            GiaHanComboBox.Items.Add("1 tuần");
            GiaHanComboBox.Items.Add("2 tuần");
            GiaHanComboBox.Items.Add("3 tuần");
            GiaHanComboBox.Items.Add("1 tháng");
        }

        private void maPhieuThuCuTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            PHIEUTHU _phieuThu = new PHIEUTHU();
            _phieuThu = _selectInfo.SelectPhieuThu(maPhieuThuCuTextBox.Text);
            maDangKyCuTextBox.Text = _phieuThu.MaDangKy;
            soTienThuCuTextBox.Text = _phieuThu.SoTien.ToString();
            ngayThuCuDatePicker.SelectedDate = _phieuThu.NgayThu;
            ngayHetHanCuDatePicker.SelectedDate = _phieuThu.NgayHetHan;
            GroupGiaHan.IsEnabled = true;
        }

        private void GiaHanComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //string _giaHan = GiaHanComboBox.SelectedItem.ToString();
            switch (GiaHanComboBox.SelectedItem.ToString())
            {
                case "1 tuần":
                    ngayHetHanMoiDatePicker.SelectedDate = ngayHetHanCuDatePicker.SelectedDate.Value.AddDays(7); 
                    break;
                case "2 tuần":
                    ngayHetHanMoiDatePicker.SelectedDate = ngayHetHanCuDatePicker.SelectedDate.Value.AddDays(14);
                    break;
                case "3 tuần":
                    ngayHetHanMoiDatePicker.SelectedDate = ngayHetHanCuDatePicker.SelectedDate.Value.AddDays(21);
                    break;
                case "1 tháng":
                    ngayHetHanMoiDatePicker.SelectedDate = ngayHetHanCuDatePicker.SelectedDate.Value.AddMonths(1);
                    break;
            }
            maPhieuThuMoiTextBox.Text = _getID.GenerateMaPhieuThu();
            maDangKyMoiTextBox.Text = maDangKyCuTextBox.Text;
            ngayThuMoiDatePicker.SelectedDate = ngayHetHanCuDatePicker.SelectedDate;
            soTienThuMoiTextBox.Text = (int.Parse(soTienThuCuTextBox.Text) * _selectInfo.SelectThamSo_TiLePhieuThuKeTiep()).ToString();
        }

        private void btnKiemTraCu_Click(object sender, RoutedEventArgs e)
        {
            PHIEUTHU _phieuThu = new PHIEUTHU();
            _phieuThu = _selectInfo.SelectPhieuThu(maPhieuThuCuTextBox.Text);
            if (_phieuThu != null)
            {
                maDangKyCuTextBox.Text = _phieuThu.MaDangKy;
                soTienThuCuTextBox.Text = _phieuThu.SoTien.ToString();
                ngayThuCuDatePicker.SelectedDate = _phieuThu.NgayThu;
                ngayHetHanCuDatePicker.SelectedDate = _phieuThu.NgayHetHan;
                GroupGiaHan.IsEnabled = true;
            }
            else MessageBox.Show("Kiểm Tra Lại Mã", "Thông Báo");
        }

        private void btnChapNhan_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                _insertInfo.InsertPhieuThu(maDangKyMoiTextBox.Text,
                    maDangKyMoiTextBox.Text,
                    DateTime.Parse(ngayThuMoiDatePicker.SelectedDate.ToString()),
                    int.Parse(soTienThuMoiTextBox.Text),
                    DateTime.Parse(ngayHetHanMoiDatePicker.SelectedDate.ToString()));
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông Báo");
            }
        }
    }
}
